﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Model.Calendar;
using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Web.Script;
using System.Text;
using Cn.Cellula.Biz;

namespace Cn.Cellula.Website.Ws.Calendar {
    public partial class _Todo : PageSupport {
        ICalendarBiz biz = BizFactory.Get<ICalendarBiz>();
        String _Action = "";
        DateTime _dt = DateTime.Now;
        String _todoKey = "";
        protected void Page_Load(object sender, EventArgs e) {
            try {
                if (!String.IsNullOrEmpty(Request.QueryString["dt"])) {
                    _dt = Convert.ToDateTime(Request.QueryString["dt"]);
                }
            } catch {
            }
            _todoKey = Request.QueryString["id"];
            _Action = String.IsNullOrEmpty(Request.QueryString["act"])?"0":Request.QueryString["act"];
            if (_Action == "0") { //进行中的
                ResponseOut(LoadActivedTodo(_dt));
            } else if (_Action == "1") {//未开始的
                ResponseOut(LoadUnStartTodo(_dt));
            } else if (_Action == "2") {//已完成的
                ResponseOut(LoadEndedTodo(_dt));
            } else if (_Action == "10") {//开始一个任务
                ResponseOut(StartTask(_todoKey));
            } else if (_Action == "11") {//结束一个任务
                ResponseOut(CloseTask(_todoKey));
            } else if (_Action == "31") {//增加一个任务
                ResponseOut(AddTask());
            } else if (_Action == "32") {//修改一个任务
                ResponseOut(UpdateTask(_todoKey));
            } else {
                ResponseOut("{\"ex\":\"WebService Error:$nUnkonwn Action\"}");
            }
        }
        private void ResponseOut(String rContent) {
            Response.Clear();
            Response.Write(rContent);
            Response.End();
        }

        #region 动作系列的操作
        private String StartTask(String rTodoKey) {
            try {
                TCalendarTodoEO eo = biz.Load<TCalendarTodoEO>(rTodoKey);
                eo.StartToReal = DateTime.Now;
                biz.Update<TCalendarTodoEO>(eo);
            } catch (Exception ex) {
                log.Error(ex.Message);
                return "[0," + ex.Message.Replace("\"", "\\\"") + "]";
            }
            return "[1,\"\"]";
        }
        private String CloseTask(String rTodoKey) {
            try {
                TCalendarTodoEO eo = biz.Load<TCalendarTodoEO>(rTodoKey);
                eo.EndToReal = DateTime.Now;
                biz.Update<TCalendarTodoEO>(eo);
            } catch (Exception ex) {
                log.Error(ex.Message);
                return "[0," + ex.Message.Replace("\"", "\\\"") + "]";
            }
            return "[1,\"\"]";
        }
        private String AddTask() {
            try {
                TCalendarTodoEO eo = new TCalendarTodoEO();
                eo.AssignToUser = base.Actor.ID;
                eo.Content = Request.QueryString["content"];
                eo.CreateDt = DateTime.Now;
                eo.EndToPlan = Convert.ToDateTime(Request.QueryString["etp"]);
                eo.IfClose = 0;
                eo.IsPrivate = 0;
                eo.LastDt = DateTime.Now;
                eo.Operator = base.Actor.ID;
                eo.OwnerKey = this.OwnerKey;
                eo.StartToPlan = Convert.ToDateTime(Request.QueryString["stp"]);
                eo.StartToReal = Convert.ToDateTime(Request.QueryString["str"]);
                eo.TodoLevel = CodeTypeDef.Todolevel.Normal;
                eo.TodoTitle = Request.QueryString["title"];
                eo.TodoType = CodeTypeDef.Todotype.Daily;
                eo.RefItemKey = Request.QueryString["item"];
                biz.Save<TCalendarTodoEO>(eo);
            } catch (Exception ex) {
                log.Error(ex.Message);
                return "[0," + ex.Message.Replace("\"", "\\\"") + "]";
            }
            return "[1,\"\"]";
        }

        private String UpdateTask(String rEoKey) {
            try {
                TCalendarTodoEO eo = biz.Load<TCalendarTodoEO>(rEoKey);
                eo.AssignToUser = base.Actor.ID;
                eo.Content = Request.QueryString["content"];
                eo.EndToPlan = Convert.ToDateTime(Request.QueryString["etp"]);
                eo.LastDt = DateTime.Now;
                eo.StartToPlan = Convert.ToDateTime(Request.QueryString["stp"]);
                eo.StartToReal = Convert.ToDateTime(Request.QueryString["str"]);
                eo.TodoTitle = Request.QueryString["title"];
                eo.RefItemKey = Request.QueryString["item"];
                biz.Save<TCalendarTodoEO>(eo);
            } catch (Exception ex) {
                log.Error(ex.Message);
                return "[0," + ex.Message.Replace("\"", "\\\"") + "]";
            }
            return "[1,\"\"]";
        }
        #endregion

        #region 列表系列的操作
        /// <summary>
        /// 进行中的
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private String LoadActivedTodo(DateTime dt) {
            String start = dt.ToString("yyyy-MM-dd") + " 00:00:00";
            String end = dt.ToString("yyyy-MM-dd") + " 23:59:59";
            String hql = "from VCalendarTodoEO eo";
            hql += " where eo.AssignToUser='"+this.Actor.ID+"' ";
            hql += " and eo.OwnerKey='" + this.OwnerKey + "' ";
            hql += " and eo.StartToReal is not null ";
            hql += " and eo.EndToReal is null ";
            hql += " and eo.StartToReal<='" + end + "' ";
            hql += " order by eo.StartToReal desc ";
            IList<Object> list = biz.Find<Object>(hql);
            return JsUtil.ParseEOList2Json(list);
        }
        /// <summary>
        /// 未开始的
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private String LoadUnStartTodo(DateTime dt) {
            String start = dt.ToString("yyyy-MM-dd") + " 00:00:00";
            String end = dt.ToString("yyyy-MM-dd") + " 23:59:59";
            String hql = "from VCalendarTodoEO eo";
            hql += " where eo.AssignToUser='" + this.Actor.ID + "' ";
            hql += " and eo.OwnerKey='" + this.OwnerKey + "' ";
            hql += " and eo.StartToReal is null ";
            hql += " order by eo.StartToPlan desc ";
            IList<Object> list = biz.Find<Object>(hql);
            return JsUtil.ParseEOList2Json(list);
        }
        /// <summary>
        /// 已完成的
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private String LoadEndedTodo(DateTime dt) {
            String start = dt.ToString("yyyy-MM-dd") + " 00:00:00";
            String end = dt.ToString("yyyy-MM-dd") + " 23:59:59";
            String hql = "from VCalendarTodoEO eo";
            hql += " where eo.AssignToUser='" + this.Actor.ID + "' ";
            hql += " and eo.OwnerKey='" + this.OwnerKey + "' ";
            hql += " and eo.EndToReal is not null ";
            hql += " and eo.EndToReal<='" + end + "' ";
            hql += " and eo.EndToReal>='" + start + "' ";
            hql += " and eo.StartToReal<='" + end + "' ";
            hql += " order by eo.EndToReal desc ";
            IList<Object> list = biz.Find<Object>(hql);
            return JsUtil.ParseEOList2Json(list);
        }
        #endregion

    }
}